package com.zsrt.oa.recharge.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zsrt.oa.recharge.dao.RechargeDao;
import com.zsrt.oa.recharge.entity.RechargeEntity;
import com.zsrt.utils.DBUtils;

/**
 * 充值持久层实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月8日
 */
public class RechargeDaoImpl implements RechargeDao {
	
	/**
	 * 增加一条充值记录
	 * @param entity
	 * @return
	 */
	@Override
	public int rechargeAdd(RechargeEntity entity) throws SQLException{
		int counter = 0;
		
		Connection con = DBUtils.getMessage();
		String sql = "INSERT INTO TKT_RECHARGE_T (USER_ID,RC_MONEY,RC_TIME) VALUES(?,?,SYSDATE())";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, entity.getUserId());
		ps.setDouble(2, entity.getRechargeMoney());
		counter = ps.executeUpdate();
		
		DBUtils.closeResources(con, ps);
		return counter;
	}
	
	/**
	 * 查询充值记录
	 * @param entity
	 * @return
	 * @throws SQLException
	 */
	@Override
	public List<RechargeEntity> rechargeQuery(RechargeEntity entity) throws SQLException {
		List<RechargeEntity> list = new ArrayList<RechargeEntity>();
		Connection con = DBUtils.getMessage();
		String sql = "SELECT U.USER_NAME as userName,"
				+ " R.RC_MONEY as rechargeMoney,"
				+ " R.RC_TIME as rechargeTime"
				+ " FROM TKT_USER_T U INNER JOIN TKT_RECHARGE_T R "
				+ " ON U.USER_PHONE = R.USER_ID "
				+ " WHERE R.USER_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, entity.getUserId());
		
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			RechargeEntity rechargeEntity = new RechargeEntity();
			rechargeEntity.setUserName(rs.getString("userName"));
			rechargeEntity.setRechargeMoney(rs.getDouble("rechargeMoney"));
			rechargeEntity.setRechargeTime(rs.getString("rechargeTime"));
			
			list.add(rechargeEntity);
		}
		
		return list;
	}

}
